/*
 * Copyright (C) 2014 Benny Bobaganoosh
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.base.engine.rendering.resourceManagement;

import static org.lwjgl.opengl.GL11.GL_DEPTH_COMPONENT;
import static org.lwjgl.opengl.GL11.GL_NONE;
import static org.lwjgl.opengl.GL11.GL_RGBA;
import static org.lwjgl.opengl.GL11.GL_TEXTURE_MAG_FILTER;
import static org.lwjgl.opengl.GL11.GL_TEXTURE_MIN_FILTER;
import static org.lwjgl.opengl.GL11.GL_UNSIGNED_BYTE;
import static org.lwjgl.opengl.GL11.glBindTexture;
import static org.lwjgl.opengl.GL11.glGenTextures;
import static org.lwjgl.opengl.GL11.glTexImage2D;
import static org.lwjgl.opengl.GL11.glTexParameterf;
import static org.lwjgl.opengl.GL11.glTexParameteri;
import static org.lwjgl.opengl.GL11.glViewport;
import static org.lwjgl.opengl.GL12.GL_TEXTURE_BASE_LEVEL;
import static org.lwjgl.opengl.GL12.GL_TEXTURE_MAX_LEVEL;
import static org.lwjgl.opengl.GL15.glDeleteBuffers;
import static org.lwjgl.opengl.GL20.glDrawBuffers;
import static org.lwjgl.opengl.GL30.GL_DRAW_FRAMEBUFFER;
import static org.lwjgl.opengl.GL30.GL_FRAMEBUFFER;
import static org.lwjgl.opengl.GL30.GL_FRAMEBUFFER_COMPLETE;
import static org.lwjgl.opengl.GL30.glBindFramebuffer;
import static org.lwjgl.opengl.GL30.glCheckFramebufferStatus;
import static org.lwjgl.opengl.GL30.glDeleteFramebuffers;
import static org.lwjgl.opengl.GL30.glFramebufferTexture2D;
import static org.lwjgl.opengl.GL30.glGenFramebuffers;

import java.nio.ByteBuffer;

public class TextureResource {
	private int[] textureId;
	private int refCount;
	private int textureTarget;
	private int numTextures;
	private int width;
	private int height;

	private int[] framebuffers;

	public TextureResource(int textureTarget, int width, int height, int numTextures,
			ByteBuffer[] imageDatas, int[] filters, int[] attachments) {
		textureId = new int[numTextures];

		this.numTextures = numTextures;
		for (int i = 0; i < numTextures; i++) {
			this.textureId[i] = glGenTextures();
		}
		this.refCount = 1;
		this.textureTarget = textureTarget;

		this.width = width;
		this.height = height;

		framebuffers = new int[numTextures];
		for (int i = 0; i < numTextures; i++) {
			framebuffers[i] = 0;
		}

		initTextures(imageDatas, filters);
		initRenderTargets(attachments);
	}

	private void initTextures(ByteBuffer[] imageDatas, int[] filters) {
		for (int i = 0; i < numTextures; i++) {
			glBindTexture(textureTarget, textureId[i]);



			glTexParameteri(textureTarget, GL_TEXTURE_BASE_LEVEL, 0);
			glTexParameteri(textureTarget, GL_TEXTURE_MAX_LEVEL, 0);
			
			glTexParameterf(textureTarget, GL_TEXTURE_MIN_FILTER, filters[i]);
			glTexParameterf(textureTarget, GL_TEXTURE_MAG_FILTER, filters[i]);

			glTexImage2D(textureTarget, 0, GL_RGBA, width, height, 0, GL_RGBA,
					GL_UNSIGNED_BYTE, imageDatas[i]);
		}
	}

	private void initRenderTargets(int[] attachments) {
		if (attachments == null) {
			return;
		}

		 int[] drawBuffers = new int[numTextures];
//		IntBuffer drawBuffers = BufferUtils.createIntBuffer(numTextures);

		for (int i = 0; i < numTextures; i++) {
			if (attachments[i] == GL_DEPTH_COMPONENT) {
//				drawBuffers.put(GL_NONE);
				drawBuffers[i]=GL_NONE;
			} else {
//				drawBuffers.put(attachments[i]);
				drawBuffers[i]=attachments[i];
			}

			// select textures that are a render target
			if (attachments[i] == GL_NONE) {
				continue;
			}
			if (framebuffers[i] == 0) {
				framebuffers[i] = glGenFramebuffers();
				glBindFramebuffer(GL_DRAW_FRAMEBUFFER, framebuffers[i]);
				glViewport(0,0,width,height);
			}

			glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, attachments[i], textureTarget,
					textureId[i], 0);
		}
		// tell open gl to write the fragment shader output and write it to the
		// framebuffer with the attachments

		// TODO make glDrawBuffers work
		for (int i = 0; i < numTextures; i++) {
			glDrawBuffers(drawBuffers[i]);
		}
			if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
				System.err.println("Framebuffer creation has failed");
				new Exception().printStackTrace();
				System.exit(1);
			}
		
	}

	public void bind(int textureNum) {
		glBindTexture(textureTarget, textureId[textureNum]);
	}

	@Override
	protected void finalize() {
		for (int i = 0; i < numTextures; i++) {
			glDeleteBuffers(textureId[i]);

			if (framebuffers[i] != 0) {
				glDeleteFramebuffers(framebuffers[i]);
			}
		}

	}

	public void addReference() {
		refCount++;
	}

	public boolean removeReference() {
		refCount--;
		return refCount == 0;
	}
}
